package com.sx.pipeline.ui.territory

import android.graphics.Color
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.tabs.TabLayoutMediator
import com.hjq.bar.TitleBar
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYBaseAppBarActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActTerritoryPageBinding
import com.sx.pipeline.ui.territory.adapter.TerritoryPageAdapter

/**
 * @Author: YY
 * @Date: 2022/8/3
 * @Desc: 负责区域 Page
 */
@Route(path = RoutePath.Territory.TERRITORY_PAGE)
class TerritoryPageActivity: CYBaseAppBarActivity<ActTerritoryPageBinding, TerritoryVM>(R.layout.act_territory_page) {

    @Autowired(name = IntentKey.TYPE)
    @JvmField
    var type : Int? = 0

    // 任务页面
    private lateinit var territoryPageAdapter: TerritoryPageAdapter

    override fun initAppBar() {
        getTitleBar().title = "负责区域"
        getTitleBar().rightTitle = "导入"
        getTitleBar().setRightTitleSize(13f)
        getTitleBar().setRightTitleColor(Color.parseColor("#0188FF"))
    }

    override fun onRightClick(titleBar: TitleBar?) {
        ARouter.getInstance().build(RoutePath.Mine.IMPORT_FILES).navigation()
    }

    override fun initData() {
        super.initData()
        // 初始化Tab
        initPager()
    }

    /**
     * 初始化ViewPager
     */
    private fun initPager() {
        if (mBinding.viewPager.adapter == null) {
            territoryPageAdapter = TerritoryPageAdapter(
                mViewModel.getTerritoryTypeList(), supportFragmentManager, lifecycle
            )
        }
        mBinding.viewPager.offscreenPageLimit = mViewModel.getTerritoryTypeList().size
        mBinding.viewPager.adapter = territoryPageAdapter
        //绑定tabLayout和viewPager
        TabLayoutMediator(
            mBinding.tab, mBinding.viewPager,
            true, true
        ) { tab, position ->
            tab.text = mViewModel.getTerritoryTypeList()[position]
        }.attach()
        // 默认选择
        mBinding.viewPager.setCurrentItem(type!!, false)
    }
}